﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HRProject.Code.DTO;
using DTOSale = HRProject.Code.DTO.Salary.Sale;
using HRProject.Code.DAO;
using DAOSale = HRProject.Code.DAO.Salary.Sale;
using HRProject.Code.Common;
using System.Text;
using System.Collections.Generic;

namespace HRProject.Salary.Sale
{
    public partial class AjaxBasicSalary : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string sAction = Request.Params["action"];
            string sJobTitleId = Request.Params["jobTitleId"];
            string sPageIndex = Request.Params["pageIndex"];

            string sAgentId = Request.Params["agentId"];

            StringBuilder result = new StringBuilder();
            int errorCode = 0;
            string error = string.Empty;
            string status = "true";

            try
            {
                if ("get_sale_basic_salary".Equals(sAction))
                {
                    Guid jobTitleId = new Guid(sJobTitleId);
                    int pageIndex = int.Parse(sPageIndex);

                    BLJobTitle jobTitleFilter = new BLJobTitle();
                    jobTitleFilter.Id = jobTitleId;

                    AgentDAO agentDao = new AgentDAO();
                    int totalAgents = agentDao.CountAgent(jobTitleFilter, Guid.Empty, string.Empty, Constant.DefaultMinDate, Constant.DefaultMaxDate);
                    int totalPage = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(totalAgents) / Convert.ToDouble(ContentProvider.PageSize)));

                    if (pageIndex < 1)
                    {
                        pageIndex = 1;
                    }
                    if (pageIndex > totalPage)
                    {
                        pageIndex = totalPage;
                    }

                    DAOSale.BasicSalaryDAO basicSalaryDao = new DAOSale.BasicSalaryDAO();
                    DTOSale.BLBasicSalaryTable basicSalaryTable = basicSalaryDao.Collect(jobTitleFilter, Guid.Empty, string.Empty,
                        totalPage, ContentProvider.PageSize, pageIndex);

                    StringBuilder list = new StringBuilder();
                    int j = 0;
                    string format = new string('0', Convert.ToString(basicSalaryTable.AgentBasicSalaryInfos.Count).Length);
                    list.Append("[");
                    foreach (DTOSale.BLAgentBasicSalary info in basicSalaryTable.AgentBasicSalaryInfos)
                    {
                        if (j > 0)
                        {
                            list.Append(",");
                        }
                        list.Append("{\"agentId\":\"");
                        list.Append(info.Agent.Id);
                        list.Append("\",\"order\":\"");
                        list.Append(info.Order.ToString(format));
                        list.Append("\",\"fullName\":\"");
                        list.Append(info.Agent.FullName);
                        list.Append("\",\"phone\":\"");
                        list.Append(info.Agent.Phone);
                        list.Append("\",\"atmSerial\":\"");
                        list.Append(info.Agent.BankAccount.AtmSerial);
                        list.Append("\",\"bankName\":\"");
                        list.Append(info.Agent.BankAccount.BankName);
                        list.Append("\",\"baseSalary\":\"");
                        list.Append(info.SalaryString);
                        list.Append("\",\"moneyLimit\":\"");
                        list.Append(info.MoneyLimitString);
                        list.Append("\",\"marketId\":\"");
                        list.Append(info.SuperMarket.InfoNumberValue);
                        list.Append("\",\"marketName\":\"");
                        list.Append(info.SuperMarket.InfoStringValue);
                        list.Append("\"}");
                        j++;
                    }
                    list.Append("]");

                    result.Append("{\"status\":true,\"totalPage\":\"");
                    result.Append(totalPage);
                    result.Append("\",\"pageIndex\":\"");
                    result.Append(pageIndex);
                    result.Append("\",\"list\":");
                    result.Append(list);
                    result.Append("}");
                }
                else if ("search_markets".Equals(sAction))
                {
                    string sFullName = Request.Params["query"];
                    result.Append("[");
                    DAOSale.MarketDAO marketDAO = new DAOSale.MarketDAO();
                    DTOSale.BLSuperMarket filter = new DTOSale.BLSuperMarket();
                    filter.OrgName = sFullName;
                    filter.MarketName = sFullName;
                    List<DTOSale.BLSuperMarket> markets = marketDAO.Search(filter);
                    for (int i = 0; i < markets.Count; i++)
                    {
                        if (i > 0)
                        {
                            result.Append(",");
                        }
                        result.Append("{");
                        result.Append("\"value\":\"" + markets[i].FullName + "\"");
                        result.Append(",\"data\":{");
                        result.Append("\"marketId\":\"" + markets[i].MarketId + "\"");
                        result.Append(",\"fullName\":\"" + markets[i].FullName + "\"");
                        result.Append("}");
                        result.Append("}");
                    }
                    result.Append("]");
                    result.Insert(0, "{\"query\": \"Li\", \"suggestions\":");
                    result.Append("}");
                }
                else if ("update_basic_salary".Equals(sAction))
                {
                    string sBaseSalary = Request.Params["baseSalary"];
                    string sMoneyLimit = Request.Params["moneyLimit"];
                    string sPhone = Request.Params["phone"];
                    string sAtmSerial = Request.Params["atmSerial"];
                    string sBankName = Request.Params["bank"];
                    string sMarketId = Request.Params["marketId"];
                    string sMarketName = Request.Params["marketName"];
                    int baseSalary, moneyLimit, marketId;
                    if (int.TryParse(sBaseSalary, out baseSalary)
                        && int.TryParse(sMoneyLimit, out moneyLimit))
                    {
                        int updateResult = 0;
                        DTOSale.BLBasicSalaryTable monthRef = new DTOSale.BLBasicSalaryTable();
                        DTOSale.BLAgentBasicSalary basicSalaryInfo = new DTOSale.BLAgentBasicSalary();
                        basicSalaryInfo.Agent.Id = new Guid(sAgentId);
                        basicSalaryInfo.Salary.InfoNumberValue = baseSalary;
                        basicSalaryInfo.MoneyLimit.InfoNumberValue = moneyLimit;
                        basicSalaryInfo.Agent.Phone = sPhone;
                        basicSalaryInfo.Agent.BankAccount = new BLBankAccount();
                        basicSalaryInfo.Agent.BankAccount.AtmSerial = sAtmSerial;
                        basicSalaryInfo.Agent.BankAccount.BankName = sBankName;
                        if (int.TryParse(sMarketId, out marketId))
                        {
                            basicSalaryInfo.SuperMarket.InfoNumberValue = marketId;
                        }
                        basicSalaryInfo.SuperMarket.InfoStringValue = sMarketName;
                        DAOSale.BasicSalaryDAO basicSalaryDao = new DAOSale.BasicSalaryDAO();
                        updateResult = basicSalaryDao.Update(basicSalaryInfo, ref errorCode);
                        if (errorCode == 12)
                        {
                            error = ContentProvider.GetMessage("NotAuthorizedDetail")
                                .Replace("{0}", ContentProvider.GetString("UpdateSaleBasicSalary"));
                            status = "false";
                        }
                        result.Append("{\"status\":");
                        result.Append(status);
                        result.Append(",\"error\":\"");
                        result.Append(error);
                        if ("true".Equals(status))
                        {
                            result.Append("\",\"baseSalary\":\"");
                            result.Append(basicSalaryInfo.SalaryString);
                            result.Append("\",\"moneyLimit\":\"");
                            result.Append(basicSalaryInfo.MoneyLimitString);
                            result.Append("\",\"phone\":\"");
                            result.Append(basicSalaryInfo.Agent.Phone);
                            result.Append("\",\"atmSerial\":\"");
                            result.Append(basicSalaryInfo.Agent.BankAccount.AtmSerial);
                            result.Append("\",\"bankName\":\"");
                            result.Append(basicSalaryInfo.Agent.BankAccount.BankName);
                            result.Append("\",\"marketId\":\"");
                            result.Append(basicSalaryInfo.SuperMarket.InfoNumberValue);
                            result.Append("\",\"marketName\":\"");
                            result.Append(basicSalaryInfo.SuperMarket.InfoStringValue);
                        }
                        result.Append("\"}");
                    }
                    else
                    {
                        status = "false";
                        error = ContentProvider.GetMessage("InvalidNumberFormat");
                        result.Append("{\"status\":");
                        result.Append(status);
                        result.Append(",\"error\":\"");
                        result.Append(error);
                        result.Append("\"}");
                    }
                }
            }
            catch (Exception exp)
            {
                Console.Out.WriteLine(exp.Message);
            }

            Response.Clear();
            Response.ContentType = "application/json; charset=utf-8";
            Response.Write(result);
            Response.End();
        }
    }
}
